package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.ExamDao;
import com.aaa.entity.ExamEntity;
import com.aaa.entity.Pager;
import com.aaa.entity.ScoreEntity;

import java.util.List;
import java.util.Map;

/**
 * fileName:ExamDaoImpl
 * description:
 * author:王宇航
 * createTime:2019/10/10 14:00
 * version:1.0.0
 */
public class ExamDaoImpl extends BaseDao implements ExamDao {
    @Override
    public Pager queryAll(int pageNo, int pageSize) {
        String sql="select *from exam where del_flag=0 limit ?,?";
        Object[] params=new Object[]{(pageNo-1)*pageSize,pageSize};
        List<Map> list=this.executeQuery(sql,params);
        Integer totalCount= Integer.valueOf(queryCount().toString());
        Pager pager=new Pager(pageNo,pageSize,list,totalCount);
        return pager;
    }

    @Override
    public Map queryOne(int id) {
        String sql="select *from exam where id=?";
        List<Map> map=this.executeQuery(sql,id);
        return map.size()>0?map.get(0):null;
    }

    @Override
    public int exam_add(ExamEntity exam) {
        String sql="insert into exam (exam_name,date) values(?,?)";
        return this.executeUpdate(sql,exam.getExamName(),exam.getDate());
    }

    @Override
    public int exam_update(ExamEntity exam) {
        String sql="update exam set exam_name=? ,date=? where id=?";
        return this.executeUpdate(sql,exam.getExamName(),exam.getDate(),exam.getId());
    }

    @Override
    public int exam_delete(int id) {
        String sql="update exam set del=1 where id=?";
        return this.executeUpdate(sql,id);
    }

    @Override
    public int updateState(int id, int state) {
        String sql="update exam set state=? where id=?";
        return this.executeUpdate(sql,state,id);
    }

    @Override
    public List queryClass() {
        String sql="select classid,classname from class where del_flag=0 and progressing=0";
        return this.executeQuery(sql);
    }

    @Override
    public List queryStu(int classid) {
        String sql="select stuid,stuname from student where classid=?";
        return this.executeQuery(sql,classid);
    }

    @Override
    public int addScore(ScoreEntity score) {
        String sql="insert into score (stuid,exam_id,scoce,class_id,stu_name) values(?,?,?,?,?)";

        return this.executeUpdate(sql,score.getStuid(),score.getExamId(),score.getScoce(),score.getClassId(),score.getStuName());
    }


    public Long queryCount(){
        String sql="select count(*) cnt from exam";
        List<Map> list=this.executeQuery(sql,null);
        return (Long) list.get(0).get("cnt");
    }
}
